const { query } = require("../db/query")
module.exports = {
  // 通过用户名查询用户
  findUserOfUsername: async (username) => {
    // 调用query发送sql语句到mysql进行查询用户
    return await query("select * from user where username = ?", [username])
  },
  // 通过手机号查询用户
  findUserOfMobile: async (mobile) => {
    // 调用query发送sql语句到mysql进行查询用户
    return await query("select * from user where mobile = ?", [mobile])
  },
  // 注册用户
  register: async (username, password, email, mobile, role_id) => {
    // 调用query发送sql语句到mysql进行注册用户
    return await query("insert into user(username,password,email,mobile,role_id) VALUES(?,?,?,?,?)", [username, password, email, mobile, role_id])
  },
  // 编辑用户
  editUser: async (user_id, username, password, email, mobile, role_id, status) => {
    // 调用query发送sql语句到mysql进行编辑用户
    return await query("update user set username = ?,password = ?,email = ?,mobile = ?,role_id = ?,status =? where id = ?", [username, password, email, mobile, role_id, status, user_id])
  },
  // 获取全部用户
  getAllUser: async () => {
    // 调用query发送sql语句到mysql进行查询用户
    return await query("select * from user");
  },
  // 查询用户列表
  getUserList: async (current, pageSize) => {
    const currentSize = (current - 1) * pageSize;
    // 调用query发送sql语句到mysql进行查询用户
    return await query("select id,username,mobile,email,role_id,status from user limit ?,?;", [currentSize, parseInt(pageSize)])
  },
  // 通过手机号和用户名查询用户
  findUser: async (username, mobile) => {
    // 调用query发送sql语句到mysql进行查询用户
    return await query("select id,username,mobile,email,role_id,status from user where username=? or mobile =?", [username, mobile])
  },
  // 通过id删除用户
  delUser: async (id) => {
    // 调用query发送sql语句到mysql进行删除用户
    return await query("delete from user where id=?", [id])
  },
  // 通过id查询用户
  findUserOfId: async (id) => {
    // 调用query发送sql语句到mysql进行通过id查询用户
    return await query("select * from user where id=?", [id])
  },
  // 修改头像
  updateAvatar: async (id, avatar) => {
    // 调用query发送sql语句到mysql进行 修改头像
    return await query("update user set avatar =? where id =?", [avatar, id])
  },
}